<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图-基础版</title>
    <style>
        .wrapper {
            width: 600px;
            height: 400px;
            overflow: hidden;
            margin: 0 auto;
            position: relative;
        }
        .img-box {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
        .img-box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .btns {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 100%;
            display: flex;
            justify-content: space-between;
            padding: 0 10px;
            box-sizing: border-box;
        }
        .btns .pre,
        .btns .next {
            cursor: pointer;
            user-select: none;
            font-size: 50px;
            transition: 300ms;
        }
        .btns .pre {
            transform: translateX(-120%);
        }
        .btns .next {
            transform: translateX(120%);
        }
        .wrapper:hover .btns span {
            transform: translateX(0);
        }

    </style>
</head>
<body>
    <div class="wrapper">
        <div class="img-box">
            <img src="./img/case20/01.png" alt="">
        </div>
        <div class="btns">
            <span class="pre">&lt;</span>
            <span class="next">&gt;</span>
        </div>
    </div>
    <script>
        let arr = [
            "./img/case20/01.png",
            "./img/case20/02.png",
            "./img/case20/03.png",
            "./img/case20/04.png"
        ]
        const pre = document.querySelector(".pre")
        const next = document.querySelector(".next")
        const img = document.querySelector(".img-box img")
        const wrapper = document.querySelector(".wrapper")
        // 保存当前显示图片的下标值
        let curIndex = 0;
        // 上一张
        pre.addEventListener("click",function(){
            if(curIndex==0){
                curIndex = arr.length-1
            }else{
                curIndex--
            }
            img.src = arr[curIndex]
        })
        // 下一张
        next.addEventListener("click",function(){
            if(curIndex==arr.length-1){
                curIndex = 0
            }else{
                curIndex++
            }
            img.src = arr[curIndex]
        })

        // 自动播放
        let timer = null
        function startInterval(){
            timer = setInterval(() => {
                next.click()
            }, 2000);
        }
        startInterval()

        // 暂停播放
        wrapper.addEventListener("mouseenter", function(){
            clearInterval(timer)
        })
        // 重新播放
        wrapper.addEventListener("mouseleave", function(){
            startInterval()
        })
        
    </script>
</body>
</html>